#! /bin/sh
#
# chkconfig: - 84 16
# description:  PHP FastCGI Process Manager
# processname: php-fpm
# config: /etc/php-fpm.conf
# pidfile: /var/run/php-fpm/php-fpm.pid

# Standard LSB functions
#. /lib/lsb/init-functions

# Source function library.
. /etc/init.d/functions

# Check that networking is up.
. /etc/sysconfig/network

if [ "$NETWORKING" = "no" ]
then
    exit 0
fi

RETVAL=0

bin_path="/usr/bin/"
prog_init="/usr/local/lxlabs/kloxo/init"

if [ -f ${prog_init}/website_php_type ] ; then
    php_type=$(cat ${prog_init}/website_php_type)
else
    php_type="single"
    cat 'single' > ${prog_init}/website_php_type
fi

if [ ! -f ${prog_init}/website_php_used ] ; then
    cat 'php' > ${prog_init}/website_php_used
fi
    
start () {
    if [ "(${php_type}" == "multiple" ] ; then
        for i in ${bin_path}/php*m-fpm ;  do
            prog="${i#$bin_path}"
            fpmconf="/opt/configs/phpcfg/fpm/conf/${prog}.conf"
            pidfile="/var/run/php-fpm/${prog}.pid"
            lockfile="/var/lock/subsys/${prog}"
            
            echo -n $"Starting $prog: "

            daemon --pidfile ${pidfile} ${prog} --fpm-config ${fpmconf}

            RETVAL=$?
            echo
            [ $RETVAL -eq 0 ] && touch ${lockfile}
        done
    else
        prog=$(cat ${prog_init}/website_php_used)"-fpm"
        fpmconf="/opt/configs/phpcfg/fpm/conf/${prog}.conf"
        pidfile="/var/run/php-fpm/${prog}.pid"
        lockfile="/var/lock/subsys/${prog}"
 
        echo -n $"Starting $prog: "
    
        daemon --pidfile ${pidfile} ${prog} --fpm-config ${fpmconf}

        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch ${lockfile}
    fi
}
stop () {
    if [ "(${php_type}" == "multiple" ] ; then
        for i in ${bin_path}/php*m-fpm ;  do
            prog="${i#$bin_path}"
            pidfile="/var/run/php-fpm/${prog}.pid"
            
            echo -n $"Stopping $prog: "
        
            killproc -p ${pidfile} ${prog}

            RETVAL=$?
            echo
            if [ $RETVAL -eq 0 ] ; then
                rm -f ${lockfile} ${pidfile}
            fi
        done
    else
        prog=$(cat ${prog_init}/website_php_used)"-fpm"
        pidfile="/var/run/php-fpm/${prog}.pid"

        echo -n $"Stopping $prog: "

        killproc -p ${pidfile} ${prog}

        RETVAL=$?
        echo
        if [ $RETVAL -eq 0 ] ; then
            rm -f ${lockfile} ${pidfile}
        fi
    fi
}

restart () {
    stop
    start
}

reload () {
    if [ "(${php_type}" == "multiple" ] ; then
        for i in ${bin_path}/php*m-fpm ;  do
            prog="${i#$bin_path}"
            pidfile="/var/run/php-fpm/${prog}.pid"

            echo -n $"Reloading $prog: "

            killproc -p ${pidfile} ${prog} -USR2
            RETVAL=$?
            echo
        done
    else
        prog=$(cat ${prog_init}/website_php_used)"-fpm"
        pidfile="/var/run/php-fpm/${prog}.pid"

        echo -n $"Reloading $prog: "

        killproc -p ${pidfile} ${prog} -USR2
        RETVAL=$?
        echo
    fi
}

# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    if [ "(${php_type}" == "multiple" ] ; then
        for i in ${bin_path}/php*m-fpm ;  do
            prog="${i#$bin_path}"
            pidfile="/var/run/php-fpm/${prog}.pid"

            status -p ${pidfile} ${prog}
            RETVAL=$?
        done
    else
        prog=$(cat ${prog_init}/website_php_used)"-fpm"
        pidfile="/var/run/php-fpm/${prog}.pid"

        status -p ${pidfile} ${prog}
        RETVAL=$?
    fi
    ;;
  restart)
    restart
    ;;
  reload|force-reload)
    reload
    ;;
  condrestart|try-restart)
    [ -f ${lockfile} ] && restart || :
    ;;
  *)
    echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart|try-restart}"
    RETVAL=2
        ;;
esac

exit $RETVAL
